using WXWorkSvc.Common.Models;

namespace WXWorkSvc.Private.Areas.Pages.Setup.User;

public class EditModel : PageModel
{
    private readonly WXDbContext db;
    private readonly WXWorkConfig _Config;
    private readonly IRole _Role;

    [BindProperty]
    public TPrivateUser Item { get; set; } = default!;
    [BindProperty]
    public List<RoleItem> Items { get; set; } = default!;

    public EditModel(WXDbContext dbc,WXWorkConfig config,IRole r)
    {
        db = dbc;
        _Config = config;
        _Role = r;
    }
    public async Task OnGet(  string id)
    {
        Item = (await db.PrivateUsers.FindAsync(_Config.AgentId, id))!;
        Items = _Role.GetRoleItems(Item.Roles);
 
    }
    public async Task<IActionResult> OnPostAsync()
    {
        var roles = _Role.GetRoles(Items);


        await db.PrivateUsers.Where(ii => ii.AgentId==_Config.AgentId && ii.UserId==Item.UserId).ExecuteUpdateAsync(ii => ii.SetProperty(jj => jj.Roles, roles).SetProperty(ii=>ii.Ext,Item.Ext));
        return RedirectToPage("./Index");

    }
}
